package com.zhf.demo01.dao;

import com.zhf.demo01.model.User;
import com.zhf.demo01.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/13 19:16
 */
public class UserDao {

    public int createUser(User user) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            //获取连接
            connection = DbUtil.getConnection();
            //准备SQL语句
            String sql="insert into t_user values(null,?,?)";
            //获取preparestatement类型的引用
            preparedStatement = connection.prepareStatement(sql);
            //添加参数
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getPassword());
            //执行SQL语句
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            DbUtil.closeCon(connection,preparedStatement);
        }
        return 0;
    }
}
